package com.hyzx.qbasic.admin.domain.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.po.ProcessDefinitionPO;
import com.hyzx.qbasic.admin.model.po.ProcessDefinitionPagedInfoPO;
import com.hyzx.qbasic.admin.model.po.ProcessTaskDefinitionPO;
import com.hyzx.qbasic.admin.domain.dao.ProcessConfigMapper;
import com.hyzx.qbasic.admin.domain.dao.ProcessDefinitionMapper;
import com.hyzx.qbasic.admin.domain.repository.IProcessDefinitionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 黄江华 on 2018/11/22
 */

@Repository
public class ProcessDefinitionRepositoryImpl implements IProcessDefinitionRepository {

    @Autowired
    private ProcessDefinitionMapper definitionMapper;

    @Autowired
    private ProcessConfigMapper configMapper;

    @Override
    public PageInfo<ProcessDefinitionPagedInfoPO> getPagedProcessDefinitionList(int pageNum, int pageSize, Integer processCategoryId, String processDefinitionName) {
        PageInfo pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> definitionMapper.getPagedProcessDefinitionList(processCategoryId, processDefinitionName));

        return pageInfo;
    }

    @Override
    public List<ProcessDefinitionPagedInfoPO> getProcessDefinitionListByCategoryId(Integer processCategoryId) {
        return definitionMapper.getProcessDefinitionListByCategoryId(processCategoryId);
    }

    @Override
    public List<ProcessDefinitionPagedInfoPO> getProcessDefinitionListByCategoryKey(String processCategoryKey) {
        return definitionMapper.getProcessDefinitionListByCategoryKey(processCategoryKey);
    }

    @Override
    public ProcessDefinitionPO getProcessDefinition(int processDefinitionId) {
        return definitionMapper.getProcessDefinition(processDefinitionId);
    }

    @Override
    public boolean addProcessDefinition(ProcessDefinitionPO record) {
        return definitionMapper.addProcessDefinition(record) > 0;
    }

    @Override
    @Transactional
    public boolean deleteProcessDefinition(List<Integer> processDefinitionIdList) {
        boolean success = definitionMapper.deleteProcessDefinition(processDefinitionIdList) > 0;
        if (success) {
            configMapper.deleteProcessDefinition(processDefinitionIdList);
        }

        return success;
    }

    @Override
    public boolean editProcessDefinition(ProcessDefinitionPO record) {
        return definitionMapper.editProcessDefinition(record) > 0;
    }

    @Override
    public boolean addProcessDefinitionTask(List<ProcessTaskDefinitionPO> tasks) {
        return definitionMapper.addProcessDefinitionTask(tasks) > 0;
    }

    @Override
    public List<ProcessTaskDefinitionPO> getTaskDefinition(Integer processDefinitionId) {
        return definitionMapper.getTaskDefinition(processDefinitionId);
    }

    @Override
    public boolean deleteTaskDefinition(Integer processDefinitionId) {
        return definitionMapper.deleteTaskDefinition(processDefinitionId) > 0;
    }
}
